package com.pd.spider.index;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;

@Mapper
@Repository("resultRepository")
public interface ResultRepository {
    //获取所有网站类别
    @Select("select distinct WEB_SORT from TENCENTAPI.PD_SPIDER_ENUM")
    List<HashMap<String, Object> > GetSortList();

    //根据网站类别获取所有网站
    @Select("select distinct WEB_NAME from TENCENTAPI.PD_SPIDER_ENUM where WEB_SORT = #{sort}")
    List<HashMap<String, Object> > GetNameList(String sort);

    //向任务表中插入数据(商标、专利)
    @Insert("insert into TENCENTAPI.PD_SPIDER_TASK (SPIDER_OBJECT,WEB_SORT,WEB_NAME,CREATE_TIME) values (#{object},#{sort},#{name},sysdate)")
    int InsertTask(String object,String sort,String name);

    //向任务表中插入数据(商标、专利)
    @Insert("insert into TENCENTAPI.PD_SPIDER_COMPANY (NAME,URL,CREATE_TIME) values (#{name},#{url},sysdate)")
    int InsertCompany(String name,String url);

    //获取所有任务(视频、购物)
    @Select("select SPIDER_OBJECT,WEB_SORT,WEB_NAME from TENCENTAPI.PD_SPIDER_TASK")
    List<HashMap<String, Object> > GetAllTask();

    //获取所有结果记录数据相关字段(商标、专利)
    @Select("select distinct ${content} from TENCENTAPI.${tableName}")
    List<HashMap<String, Object> > GetCompany(String tableName,String content);

    //查找任务名称对应的id
    @Select("select ID from TENCENTAPI.PD_SPIDER_TASK where SPIDER_OBJECT = #{object} and WEB_NAME = #{name}")
    int GetTaskID(String object,String name);

    //根据id查找结果记录数据(视频、购物)
    @Select("select * from TENCENTAPI.${tableName} where TASK_ID = #{id}")
    List<HashMap<String, Object> > GetResult(String tableName,int id);

    //根据条件获取信息(商标)
    @Select("<script>" +
            "select * from TENCENTAPI.PD_SPIDER_BRAND " +
            "where 1=1" +
            "<if test='name!=\"\"'>" +
            "and COMPANY_NAME like '%${name}%'" +
            "</if>" +
            "<if test='describe!=\"\"'>" +
            "and INFRINGE_DESCRIPT = #{describe}" +
            "</if>" +
            "order by get_time desc" +
            "</script>")
    List<HashMap<String, Object> > GetBrandResult(String name,String describe);

    //根据条件获取信息(专利)
    @Select("<script>" +
            "select * from TENCENTAPI.PD_SPIDER_PATENT " +
            "where 1=1" +
            "<if test='name!=\"\"'>" +
            "and NAME like '%${name}%'" +
            "</if>" +
            "<if test='describe!=\"\"'>" +
            "and PREDICT = #{describe}" +
            "</if>" +
            "order by get_time desc" +
            "</script>")
    List<HashMap<String, Object> > GetPatentResult(String name,String describe);
}
